package com.shakespace.artofandroid.chapter01.launchmode

import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.shakespace.artofandroid.R
import com.shakespace.firstlinecode.global.start
import kotlinx.android.synthetic.main.activity_a.*

@Suppress("UNUSED_VARIABLE")
class AActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_a)


        tv_A.setOnClickListener {
            start(BActivity::class.java)
        }


        tv_B.setOnClickListener {
            val intent = Intent(this, BActivity::class.java)
            intent.flags = Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT
            startActivity(intent)
        }

        val intent = Intent()
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
        intent.flags = Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
        intent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP
        intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
        intent.flags = Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT  //  seems  not work by user
        intent.flags = Intent.FLAG_ACTIVITY_REORDER_TO_FRONT  //  if current is A B C , C start B with FLAG_ACTIVITY_REORDER_TO_FRONT , then stack will be A C B

        // 2020-01-07 NOTE: MATCH_DEFAULT_ONLY mean only match "android.intent.category.DEFAULT"
        // if intent-filter isnot contain "android.intent.category.DEFAULT"  , canot receive implicit intent
        val resolveActivity = packageManager.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY)

        // or
        val activity = intent.resolveActivity(packageManager)
    }
}
